package it.koutifaris.travianmap.ui;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import it.koutifaris.travianmap.ui.PointToSquareHelper.SquareSizeSource;
import junit.framework.Assert;
import android.test.AndroidTestCase;

public class PointToSquareHelperTest extends AndroidTestCase {

	private static final int SQUARE_SIZE = 10;
	private PointToSquareHelper pointToSquareHelper;

	@Override
	public void setUp() {
		SquareSizeSource squareSizeMockup = mock(PointToSquareHelper.SquareSizeSource.class);
		when(squareSizeMockup.getSquareSize()).thenReturn(SQUARE_SIZE);

		pointToSquareHelper = new PointToSquareHelper(squareSizeMockup);
	}

	public void testGetSquareOnEdge() {
		for (int n = 0; n < 3; n++) {
			int square = pointToSquareHelper.getSquare(SQUARE_SIZE * n);
			Assert.assertEquals(n, square);
		}
	}

	public void testGetSquareBetweenBounds() {
		for (int n = 0; n < 3; n++) {
			int square = pointToSquareHelper.getSquare(SQUARE_SIZE * n + 2);
			Assert.assertEquals(n, square);
		}
	}

	public void testGetSquareFirstNegativeSquare() {
		int square = pointToSquareHelper.getSquare(-2);
		Assert.assertEquals(-1, square);
	}

	public void testGetSquareNegativePoint() {
		int square = pointToSquareHelper.getSquare(-SQUARE_SIZE - 2);
		Assert.assertEquals(-2, square);
	}

	public void testGetSquareCorner() {
		for (int n = 0; n < 3; n++) {
			int corner = pointToSquareHelper.getSquareCorner(n);
			Assert.assertEquals(n * SQUARE_SIZE, corner);
		}
	}

	public void testGetSquareCornerFromPoint() {
		for (int x = -3 * SQUARE_SIZE, n = -3; x < 3 * SQUARE_SIZE; x += SQUARE_SIZE, n++) {
			int corner = pointToSquareHelper.getSquareCornerFromPoint(x);
			Assert.assertEquals(n * SQUARE_SIZE, corner);
		}
	}

	public void testGetNegativeSquareCorner() {
		for (int n = 0; n >= -3; n--) {
			int corner = pointToSquareHelper.getSquareCorner(n);
			Assert.assertEquals(n * SQUARE_SIZE, corner);
		}
	}

}
